/*
+--------------------------
| FreeloaderOS v0.0.1
|  - Microkernel
+--------------------------
| Written by Alec Hussey
| Copyright (c) 2007
|  - Deus X Foundation
+---------------------------
*/

/*
 * Standard C Library
 */

#include <system.h>
#include <string.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdbool.h>

/*
Copy a location in memory.
*/
void *memcpy(void *dest, const void *src, size_t count)
{
	const char *sp = (const char*) src;
	char *dp = (char*) dest;
	
	for (; count != 0; count--)
		*dp++ = *sp++;
	
	return dest;
}

/*
Replace a section in memory with
another character.
*/
void *memset(void *dest, char val, size_t count)
{
	char *temp = (char*) dest;
	
	for (; count != 0; count--)
		*temp++ = val;
	
	return dest;
}

/*
Replace a section in memory with
another character.
*/
unsigned short *memsetw(unsigned short *dest, unsigned short val, size_t count)
{
	unsigned short *temp = (unsigned short*) dest;
	
	for (; count != 0; count--)
		*temp++ = val;
	
	return dest;
}

/*
Get the length of a string
as an integer.
*/
int strlen(const char *str)
{
	size_t retval;
	
	for (retval = 0; *str != '\0'; str++)
		retval++;
	
	return retval;
}

/*
Compare two given strings.
*/
int strcmp(const char *str1, const char *str2)
{
	unsigned int i = 0;
	
	while ((str1[i] == str2[i]) && (str1[i]) && (str2[i]))
		i ++;
	
	if ((str1[i]) || (str2[i]))
		return FALSE;
	
	return TRUE;
}

/*
Concantenate two given strings.
*/
void strcat(char *str1, char *str2)
{
	unsigned int i;
	unsigned int str1_lenval = strlen(str1);
	
	for (i = 1; i <= strlen(str2); i++)
		str1[str1_lenval+i] = str2[i-1];
}

/*
Copy the contents of one string
into another string.
*/
void strcpy(char *str1, const char *str2)
{
	unsigned int i;
	
	for (i = 0; i < strlen(str2); i++)
		str1[i] = str2[i];
	
	str1[i] = '\0';
}

/*
Find the substring of a given string.
*/
void substr(char *str, int startpos, int endpos, char *subs)
{
	unsigned int i;
	
	for (i = 0; i < endpos; i++)
		subs[i] = str[startpos+i];
	
	subs[i] = '\0';
}
